<?php

declare(strict_types=1);

namespace App\Model;


use Hyperf\DbConnection\Db;

/**
 * @property int $id
 * @property int $tr_id 帖子/回帖id
 * @property int $uid 用户id
 * @property int $type 类型 1帖子 2回帖
 * @property int $time 点赞时间
 * @property int $is_read 0未读 1已读
 * @property int $is_push 每天推送第一条，0=未推送，1=已推送
 */
class TopicZan extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'tb_topic_zan';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = [];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'tr_id' => 'integer', 'uid' => 'integer', 'type' => 'integer', 'time' => 'integer', 'is_read' => 'integer', 'is_push' => 'integer'];


    /**
     * 获取帖子点赞数据
     * @param $tid // 帖子id/回帖id
     * @param int $type 1帖子 2回帖
     * @return mixed
     */
    public function zanCount($tid, $type = 1)
    {
        $result = self::query()->where('type', $type)->where('tr_id', $tid)->count();
        return (int)$result;
    }

    /**
     * Note - uid用户 是否点赞帖子
     * DateTime 2024/7/22 11:21
     * User Admin
     * @param $uid
     * @param $tid // 帖子id/回帖id
     * @param int $type 1帖子 2回帖
     * @return int
     */
    public function isZan($uid, $tid, $type = 1)
    {
        $result = self::query()->where('uid', $uid)->where('type', $type)->where('tr_id', $tid)->count();
        return empty($result) ? 0 : 1;
    }


    /**
     * Note - 获取点赞用户
     * DateTime 2024/7/22 16:11
     * User Admin
     * @param $topic_id
     * @param $type
     * @param $limit
     * @return mixed
     */
    public function getTopicZanUser($topic_id, $type = 1, $limit = 20)
    {
        // $sql = "select a.id,a.uid,a.tr_id,u.nickname,u.sex,case when u.`head_image`!='' then u.head_image else u.head_image_new end as head_image from {$this->table} a left join tb_user u on a.uid=u.id WHERE `type`=? and `tr_id` = ? order by `time` asc limit 0," . $limit;
        // $pd = $this->createCommand($sql);
        // $pd->execute([$type, $topic_id]);
        // $result = $pd->fetchAll(PDO::FETCH_ASSOC);
        $list = self::query()->leftJoin('tb_user', 'tb_topic_zan.uid', '=', 'tb_user.id')
            ->select(['tb_topic_zan.id', 'tb_topic_zan.uid', 'tb_topic_zan.tr_id', 'tb_user.nickname', 'tb_user.sex', 'tb_user.head_image',
                Db::raw('CASE WHEN tb_user.head_image != "" THEN tb_user.head_image ELSE tb_user.head_image_new END as head_image')
            ])
            ->where('tb_topic_zan.type', '=', $type)
            ->where('tb_topic_zan.tr_id', '=', $topic_id)
            ->orderBy('tb_topic_zan.time', 'asc')
            ->limit($limit)
            ->get()
            ->toArray();

        return $list;
    }


    /**
     * Note - 记录动态点赞
     * DateTime 2024/7/22 18:37
     * User Admin
     * @param $id number 帖子id 回帖id
     * @param $uid string 用户id
     * @param $type number 1点赞 0取消点赞
     * @param $sub_type integer 1帖子 2回帖
     * @return mixed
     */
    public function add($id, $uid, $type, $sub_type)
    {
        if ($type == 1) {
            $rs = self::updateOrInsert(['tr_id' => $id, 'uid' => $uid, 'type' => $sub_type], ['is_read' => 0, 'is_push' => 0, 'time' => time()]);
        } else {
            $rs = self::query()->where('tr_id', $id)->where('uid', $uid)->where('type', $sub_type)->delete();

        }
        return $rs;
    }


}
